/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DTO;

import java.util.*;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author TrongKhoa
 */
public class DataTableDTO
{

    private String _tableName;
    Element _tableElement;
    ArrayList<DataColumnDTO> _primaryKey;
    ArrayList<ForeignKeyDTO> _foreignKeys;
    ArrayList<DataColumnDTO> _columns;
    ArrayList<DataRowDTO> _rows;

    public DataTableDTO(Element tableElement)
    {
        _tableElement = tableElement;
        _tableName = _tableElement.getAttribute("Name");

        //Khóa ngoại
        _foreignKeys = new ArrayList<>();
        NodeList foreignKeysNL = _tableElement.getElementsByTagName("ForeignKeys");
        if (foreignKeysNL.getLength() > 0)
        {
            NodeList foreignKeyNL = ((Element) foreignKeysNL.item(0)).getElementsByTagName("ForeignKey");
            for (int i = 0; i < foreignKeyNL.getLength(); i++)
            {
                Element ForeignKeyEle = (Element) foreignKeyNL.item(i);
                _foreignKeys.add(new ForeignKeyDTO(this, ForeignKeyEle));
            }
        }

        //Đọc các cột
        _columns = new ArrayList<>();
        NodeList colsNL = _tableElement.getElementsByTagName("Columns");
        if (colsNL.getLength() > 0)
        {
            NodeList colNL = ((Element) colsNL.item(0)).getElementsByTagName("Column");
            for (int i = 0; i < colNL.getLength(); i++)
            {
                Element colElement = (Element) colNL.item(i);
                _columns.add(new DataColumnDTO(this, colElement));

            }
        }

        //Khóa chính
        _primaryKey = new ArrayList<>();
        NodeList primaryKeyNL = _tableElement.getElementsByTagName("PrimaryKey");
        if (primaryKeyNL.getLength() > 0)
        {
            NodeList primarykey = ((Element) primaryKeyNL.item(0)).getElementsByTagName("Column");
            for (int i = 0; i < primarykey.getLength(); i++)
            {
                Element primaryKeyEle = (Element) primarykey.item(i);
                String primaryColumn = primaryKeyEle.getTextContent();
                for (int j = 0; j < _columns.size(); j++)
                {
                    if (_columns.get(j).getColumnName().equalsIgnoreCase(primaryColumn))
                    {
                        _primaryKey.add(_columns.get(j));
                    }
                }
            }
        }

        _rows = new ArrayList<>();
        NodeList rowsNL = _tableElement.getElementsByTagName("Rows");
        if (rowsNL.getLength() > 0)
        {
            NodeList rowNL = ((Element) rowsNL.item(0)).getElementsByTagName("Row");
            for (int i = 0; i < rowNL.getLength(); i++)
            {
                Element rowElement = (Element) rowNL.item(i);
                _rows.add(new DataRowDTO(this, rowElement));
            }
        }

    }

    @Override
    public String toString()
    {
        return _tableName;
    }

    
    
    public ArrayList<DataColumnDTO> getColumns()
    {
        return _columns;
    }

    public void setColumns(ArrayList<DataColumnDTO> _columns)
    {
        this._columns = _columns;
    }

    public ArrayList<ForeignKeyDTO> getForeignKeys()
    {
        return _foreignKeys;
    }

    public void setForeignKeys(ArrayList<ForeignKeyDTO> _foreignKeys)
    {
        this._foreignKeys = _foreignKeys;
    }

    public ArrayList<DataRowDTO> getRows()
    {
        return _rows;
    }

    public void setRows(ArrayList<DataRowDTO> _rows)
    {
        this._rows = _rows;
    }

    public String getTableName()
    {
        return _tableName;
    }

    public void setTableName(String _tableName)
    {
        this._tableName = _tableName;
    }

    public ArrayList<DataColumnDTO> getPrimaryKey()
    {
        return _primaryKey;
    }

    public void setPrimaryKey(ArrayList<DataColumnDTO> _primaryKey)
    {
        this._primaryKey = _primaryKey;
    }
    
    public void addPrimaryKey(DataColumnDTO _additionPri)
    {
        _primaryKey.add(_additionPri);
        //Chưa xử lý element
    }
    
    public void removePrimaryKey(DataColumnDTO _pri)
    {
        _primaryKey.remove(_pri);
    }
    
    
}
